package com.tianmao.swing;

import com.alibaba.fastjson.JSONObject;
import com.tianmao.swing.core.PrintPaper;
import com.tianmao.swing.core.PrintRFID;
import org.java_websocket.WebSocket;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.server.WebSocketServer;

import java.io.*;
import java.net.InetSocketAddress;

public class MyWebSocketServer extends WebSocketServer {
    public MyWebSocketServer(InetSocketAddress address) {
        super(address);
    }
    @Override
    public void onOpen(WebSocket conn, ClientHandshake handshake) {
            String filePath = "template.txt";
            String str = "";
            try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
                String currentLine;
                while ((currentLine = reader.readLine()) != null) {
                    str+=currentLine;
                }
                conn.send(str);
            } catch (IOException e) {
                e.printStackTrace();
            }
    }
    @Override
    public void onClose(WebSocket conn, int code, String reason, boolean remote) {
        // 当连接关闭时调用此方法
    }
    @Override
    public void onMessage(WebSocket conn, String message) {
        JSONObject json = JSONObject.parseObject(message);
        if(json.getString("type").equals("print") && json.getString("printoption").equals("1")){
            PrintPaper printPaper = new PrintPaper();
            printPaper.receiveJson(json.getString("data"));
        }else if(json.getString("type").equals("print") && json.getString("printoption").equals("2")){
            PrintRFID printRFID = new PrintRFID();
            printRFID.receive(json.getString("data"));
        }
        if (json.getString("type").equals("write")) {
            //写入文件
            String filePath = "template.txt";
            String contentToWrite = json.getString("data");

            try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
                writer.write(contentToWrite);
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("WriteFile receive:"+message);
            // 当服务器接收到消息时调用此方法
            System.out.println(message);
        } else if (json.getString("type").equals("read")) {
            String filePath = "template.txt";
            String str = "";
            try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
                String currentLine;
                while ((currentLine = reader.readLine()) != null) {
                    str+=currentLine;
                }
                conn.send(str);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }
    @Override
    public void onError(WebSocket conn, Exception ex) {
        // 当发生错误时调用此方法
        System.out.println(ex.getMessage());
    }
    @Override
    public void onStart() {
        // 服务器启动时调用此方法
        System.out.println("不要关闭此窗口");
    }
}